﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;

namespace zUfDsl
{
    public static class DatatableExt
    {
        public static T ToObj<T>(this DataRow dtr)
        {
            var obj = Activator.CreateInstance<T>();
            Type t = obj.GetType();
            PropertyInfo[] ps = t.GetProperties();
            foreach (PropertyInfo p in ps)
            {
                string pname = p.Name;
                if (dtr.Table.Columns.Contains(pname))
                {
                    string val = dtr[pname].ObjToString();
                    string typename = p.PropertyType.FullName;
                    if (typename.StartsWith("System.Nullable"))
                    {
                        typename = typename.Substring(0, typename.IndexOf(","));
                        typename = typename.Replace("System.Nullable`1", "");
                        typename = typename.Replace("[", "");
                    }

                    switch (typename)
                    {
                        case "System.DateTime":
                            val = val.Replace("%20", " ");
                            if (val == "") { p.SetValue(obj, null, null); }
                            else
                            {
                                var datetime = val.ObjToDecimal();
                                p.SetValue(obj, datetime, null);
                            }
                            break;
                        case "System.Double":
                            if (val == "") { p.SetValue(obj, null, null); }
                            else
                            { p.SetValue(obj, val.ObjToDouble(), null); }
                            break;

                        case "System.Decimal":
                            if (val == "") { p.SetValue(obj, null, null); }
                            else
                            { p.SetValue(obj, val.ObjToDecimal(), null); }
                            break;

                        case "System.Int32":
                            if (val == "") { p.SetValue(obj, null, null); }
                            else
                            { p.SetValue(obj, val.ObjToInt(), null); }
                            break;

                        case "System.Boolean":
                            if (val == "") { p.SetValue(obj, null, null); }
                            else
                            { p.SetValue(obj, val.ObjToBool(), null); }
                            break;
                        default:
                            if (val != null && val.Length > 0)
                            { p.SetValue(obj, val, null); }
                            else { p.SetValue(obj, null, null); }
                            break;
                    }
                }
            }
            return obj;
        }

        public static List<T> ToList<T>(this DataTable dt)
        {
            var l = new List<T>();
            for (int k = 0; k < dt.Rows.Count; k++)
            {
                var t = dt.Rows[k].ToObj<T>();
                l.Add(t);
            }
            return l;
        }
    }
}
